package com.migu.contacts.util;

import android.app.Activity;
import android.text.TextUtils;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.butterknife.OnClick;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.DateFormatter;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.entity.TimeEntity;
import com.github.gzuliyujiang.wheelpicker.impl.UnitDateFormatter;
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout;
import com.github.gzuliyujiang.wheelpicker.widget.DatimeWheelLayout;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.databinding.ContactEditDateBinding;
import com.migu.contacts.databinding.ContactEditDatetimeBinding;
import com.migu.contacts.widget.Callback;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class PopUtil {


    public static void showMonthDayPicker(com.frank.libandroid.BaseActivity mActivity, String target, Callback<String> callback) {
        LocalDate now = LocalDate.now();
        if (!TextUtils.isEmpty(target)) {
            try {
                now = LocalDate.parse(now.getYear() + "-" + target);
            } catch (Exception e) {
            }
        }
        DateEntity dateEntity = DateEntity.target(now.getYear(), now.getMonthValue(), now.getDayOfMonth());
        BottomPopup bottomPopup = new BottomPopup<ContactEditDateBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                DateWheelLayout date = mBinding.date;
                int month = date.getSelectedMonth();
                int day = date.getSelectedDay();
                callback.onComplete(String.format("%02d-%02d", month, day));
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                DateWheelLayout wheelLayout = mBinding.date;
                wheelLayout.setDateMode(DateMode.MONTH_DAY);
                wheelLayout.setDateFormatter(new UnitDateFormatter());
                wheelLayout.setRange(DateEntity.target(dateEntity.getYear(), 1, 1), DateEntity.target(dateEntity.getYear(), 12, 31));
                wheelLayout.setDefaultValue(dateEntity);
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }


    public static void showDatePicker(BaseActivity mActivity, String target, Callback<String> callback) {
        BottomPopup bottomPopup = new BottomPopup<ContactEditDateBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                DateWheelLayout date = mBinding.date;
                callback.onComplete(String.format("%04d-%02d-%02d", date.getSelectedYear(), date.getSelectedMonth(), date.getSelectedDay()));
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                mBinding.date.setDateFormatter(new UnitDateFormatter());
                LocalDate now = LocalDate.now();
                if (!TextUtils.isEmpty(target)) {
                    now = LocalDate.parse(target);
                }
                mBinding.date.setRange(DateEntity.yearOnFuture(-100), DateEntity.yearOnFuture(100));
                mBinding.date.setDefaultValue(DateEntity.target(now.getYear(), now.getMonthValue(), now.getDayOfMonth()));
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    public static void showDatePicker(Activity mActivity, Callback<String> callback) {
        BottomPopup bottomPopup = new BottomPopup<ContactEditDateBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                DateWheelLayout date = mBinding.date;
                LocalDate localDate = LocalDate.of(date.getSelectedYear(), date.getSelectedMonth(), date.getSelectedDay());
                String string = localDate.toString();
                callback.onComplete(string);
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                mBinding.date.setDateFormatter(new DateFormatter() {
                    @Override
                    public String formatYear(int year) {
                        return year + "年";
                    }

                    @Override
                    public String formatMonth(int month) {
                        return month + "月";
                    }

                    @Override
                    public String formatDay(int day) {
                        return day + "日";
                    }
                });
                mBinding.date.setRange(DateEntity.yearOnFuture(-100), DateEntity.yearOnFuture(100));
                mBinding.date.setDefaultValue(DateEntity.today());
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    public static void showDatePicker(Activity mActivity, String str, Callback<String> callback) {
        LocalDate localDate = null;
        try {
            if (!TextUtils.isEmpty(str)) {
                if (str.contains(" ")) {
                    localDate = LocalDate.parse(str.split(" ")[0]);
                } else {
                    localDate = LocalDate.parse(str);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LocalDate finalLocalDate = localDate;
        BottomPopup bottomPopup = new BottomPopup<ContactEditDateBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                DateWheelLayout date = mBinding.date;
                LocalDate localDate = LocalDate.of(date.getSelectedYear(), date.getSelectedMonth(), date.getSelectedDay());
                String string = localDate.toString();
                callback.onComplete(string);
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                mBinding.date.setDateFormatter(new DateFormatter() {
                    @Override
                    public String formatYear(int year) {
                        return year + "年";
                    }

                    @Override
                    public String formatMonth(int month) {
                        return month + "月";
                    }

                    @Override
                    public String formatDay(int day) {
                        return day + "日";
                    }
                });
//                mBinding.date.setRange(DateEntity.yearOnFuture(-100), DateEntity.yearOnFuture(100));
                if (finalLocalDate != null) {
                    DateEntity today = DateEntity.today();
                    today.setYear(finalLocalDate.getYear());
                    today.setMonth(finalLocalDate.getMonthValue());
                    today.setDay(finalLocalDate.getDayOfMonth());
                    mBinding.date.setDefaultValue(today);
                }
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    static DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * @param mActivity
     * @param str       可为null
     * @param callback
     */
    public static void showDateTimePicker(Activity mActivity, String str, Callback<String> callback) {
        LocalDateTime now = LocalDateTime.now();
        if (!TextUtils.isEmpty(str)) {
            try {
                now = LocalDateTime.parse(str, dateTimeFormatter);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                now = LocalDateTime.parse(str, dateTimeFormatter1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        LocalDateTime finalNow = now;
        BottomPopup bottomPopup = new BottomPopup<ContactEditDatetimeBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                DatimeWheelLayout date = mBinding.date;
                callback.onComplete(String.format("%04d-%02d-%02d %02d:%02d", date.getSelectedYear(), date.getSelectedMonth(), date.getSelectedDay(), date.getSelectedHour(), date.getSelectedMinute()));
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                final DatimeWheelLayout wheelLayout = mBinding.date;
                wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
                wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND);
                wheelLayout.setRange(DatimeEntity.yearOnFuture(-100), DatimeEntity.yearOnFuture(100));
                wheelLayout.setDateLabel("年", "月", "日");
                wheelLayout.setTimeLabel("时", "分", "秒");
                DatimeEntity defaultValue = new DatimeEntity();
                defaultValue.setDate(DateEntity.target(finalNow.getYear(), finalNow.getMonthValue(), finalNow.getDayOfMonth()));
                defaultValue.setTime(TimeEntity.target(finalNow.getHour(), finalNow.getMinute(), finalNow.getSecond()));
                wheelLayout.setDefaultValue(defaultValue);
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }
}
